home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / Development / PartMaker / Container•1.0d11v2 / Container•1.0d11v2.rsrc / dFRK_5121 < prev    next >
Encoding:
Text File  |  1995-12-08  |  14.5 KB  |  432 lines

  1. //========================================================================================
  2. //
  3. //    File:                ContainerDDCmd.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Author:                Mary Boetcher
  7. //
  8. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "Container.hpp"
  13.  
  14. #ifndef CONTAINERDDCMD_H
  15. #include "ContainerDDCmd.h"
  16. #endif
  17.  
  18. #ifndef CONTAINERSEL_H
  19. #include "ContainerSel.h"
  20. #endif
  21.  
  22. #ifndef BASESHP_H
  23. #include "BaseShp.h"
  24. #endif
  25.  
  26. #ifndef CONTAINERPART_H
  27. #include "ContainerPart.h"
  28. #endif
  29.  
  30. #ifndef CONTAINERCLIP_H
  31. #include "ContainerClip.h"
  32. #endif
  33.  
  34. #ifndef CONTAINERLINK_H
  35. #include "ContainerLink.h"
  36. #endif
  37.  
  38. // ----- FrameWork Includes -----
  39.  
  40. #ifndef FWFRAME_H
  41. #include "FWFrame.h"
  42. #endif
  43.  
  44. #ifndef FWPRESEN_H
  45. #include "FWPresen.h"
  46. #endif
  47.  
  48. //========================================================================================
  49. // RunTime Info
  50. //========================================================================================
  51.  
  52. #ifdef FW_BUILD_MAC
  53. #pragma segment containercommand
  54. #endif
  55.  
  56. //========================================================================================
  57. // class CDragCommand
  58. //========================================================================================
  59.  
  60. //----------------------------------------------------------------------------------------
  61. //    CDragCommand constructor
  62. //----------------------------------------------------------------------------------------
  63. CDragCommand::CDragCommand(Environment* ev,
  64.                            CContainerPart* part,
  65.                            FW_CFrame* frame,
  66.                            CContainerSelection* selection,
  67.                            FW_Boolean canUndo)
  68.     : FW_CDragCommand(ev, frame, canUndo),
  69.         fContainerPart(part),
  70.         fContainerSelection(selection),
  71.         fClearedShapeList(NULL)
  72. {
  73. }
  74.  
  75. //----------------------------------------------------------------------------------------
  76. //    CDragCommand destructor
  77. //----------------------------------------------------------------------------------------
  78. CDragCommand::~CDragCommand()
  79. {
  80.     delete fClearedShapeList;
  81. }
  82.  
  83. //----------------------------------------------------------------------------------------
  84. //    CDragCommand::SaveUndoState
  85. //----------------------------------------------------------------------------------------
  86. void CDragCommand::SaveUndoState(Environment* ev)    // Override
  87. {
  88.     fClearedShapeList = new FW_CPrivOrderedCollection;
  89.  
  90.     // Save shapes that will be dragged
  91.     FW_COrderedCollectionIterator ite(fContainerSelection->GetSelectionCollection());
  92.     for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  93.     {
  94.         fClearedShapeList->AddLast(shape);
  95.     }
  96.  
  97.     this->SetMenuStrings(ev, "Undo Move", "Redo Move");
  98. }
  99.  
  100. //----------------------------------------------------------------------------------------
  101. //    CDragCommand::UndoIt
  102. //----------------------------------------------------------------------------------------
  103.  
  104. void CDragCommand::UndoIt(Environment* ev)    // Override
  105. {
  106.     // ----- Add the dragged shapes back into the part
  107.     FW_COrderedCollectionIterator ite(fClearedShapeList);
  108.     for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  109.     {
  110.         shape->RestoreShape(ev, fContainerPart);
  111.     }
  112.  
  113.     // ----- Add the saved shapes to the selection
  114.     this->SelectDraggedShapes(ev);
  115.     
  116.     // ----- Invalidate 
  117.     fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. //    CDragCommand::RedoIt
  122. //----------------------------------------------------------------------------------------
  123.  
  124. void CDragCommand::RedoIt(Environment* ev)    // Override
  125. {
  126.     this->SelectDraggedShapes(ev);    // Select saved shapes
  127.  
  128.     fContainerSelection->ClearSelection(ev); // clear them, again
  129. }
  130.  
  131. //----------------------------------------------------------------------------------------
  132. //    CDragCommand::FreeUndoState
  133. //----------------------------------------------------------------------------------------
  134. void CDragCommand::FreeUndoState(Environment *ev)    // Override
  135. {
  136.     // Delete the dragged shapes - they're gone forever
  137.     CBaseShape* shape;
  138.  
  139.     //-- First check whether the shapes are promised
  140.     FW_COrderedCollectionIterator ite(fClearedShapeList);
  141.     for (shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  142.     {
  143.         shape->CheckPromise(ev, fContainerPart);
  144.     }
  145.  
  146.     //-- Now delete them
  147.     while ((shape = (CBaseShape*)fClearedShapeList->First()) != NULL)
  148.     {
  149.         fClearedShapeList->Remove(shape);
  150.         delete shape;
  151.     }
  152. }
  153.  
  154. //----------------------------------------------------------------------------------------
  155. //    CDragCommand::SelectDraggedShapes
  156. //----------------------------------------------------------------------------------------
  157.  
  158. void CDragCommand::SelectDraggedShapes(Environment* ev)
  159. {
  160.     // First, empty the selection
  161.     fContainerSelection->CloseSelection(ev);
  162.  
  163.     // Add the shapes in fClearedShapeList to the selection
  164.     FW_COrderedCollectionIterator ite(fClearedShapeList);
  165.     for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  166.     {
  167.         fContainerSelection->AddToSelection(ev, shape, FALSE);
  168.     }
  169. }
  170.  
  171. //========================================================================================
  172. // class CDropCommand
  173. //========================================================================================
  174.  
  175. //----------------------------------------------------------------------------------------
  176. //    CDropCommand constructor
  177. //----------------------------------------------------------------------------------------
  178. CDropCommand::CDropCommand(Environment* ev,
  179.                             CContainerPart* part,
  180.                            FW_CFrame* frame,
  181.                            ODDragItemIterator* dropInfo, 
  182.                            ODFacet* facet, 
  183.                            const FW_CPoint& windowPoint,
  184.                            FW_Boolean canUndo)
  185.     : FW_CDropCommand(ev, (FW_CPart*)part, frame, dropInfo, facet, windowPoint, canUndo),
  186.         fContainerPart(part)
  187. {
  188.     fContainerSelection = (CContainerSelection*)frame->GetPresentation(ev)->GetSelection(ev);
  189.     fDropDelta = FW_kZeroPoint;
  190.     fDroppedShapeList = new FW_CPrivOrderedCollection;
  191.     fSavedLink = NULL;
  192. }
  193.  
  194. //----------------------------------------------------------------------------------------
  195. //    CDropCommand destructor
  196. //----------------------------------------------------------------------------------------
  197. CDropCommand::~CDropCommand()
  198. {
  199.     delete fDroppedShapeList;
  200. }
  201.  
  202. //----------------------------------------------------------------------------------------
  203. //    CDropCommand::DoDrop
  204. //----------------------------------------------------------------------------------------
  205. FW_Boolean CDropCommand::DoDrop(Environment* ev, 
  206.                                 ODStorageUnit* dropSU, 
  207.                                 const FW_CPoint& mouseOffset, 
  208.                                 const FW_CPoint& dropPoint,
  209.                                 FW_Boolean isDropMove)
  210. {
  211.     fContainerSelection->CloseSelection(ev);
  212.     
  213.     FW_Boolean result = FW_CDropCommand::DoDrop(ev, dropSU, mouseOffset, dropPoint, isDropMove);
  214.     
  215.     if (result)
  216.     {    
  217.         fContainerPart->SetTool(ev, kSelectTool);
  218.         
  219.         FW_CPoint newPosition(dropPoint.x - mouseOffset.x, dropPoint.y - mouseOffset.y);
  220.         
  221.         FW_CRect box;
  222.         fContainerSelection->GetDragRect(box);
  223.  
  224.         fContainerSelection->OffsetSelection(ev, newPosition.x - box.left, newPosition.y - box.top);
  225.         
  226.         ODShape* updateShape = fContainerSelection->GetUpdateShape();
  227.         
  228.         // ----- Calculate clip -----
  229.         CContainerFacetClipper facetClipper(ev, fContainerPart);
  230.         facetClipper.Clip(ev, fContainerSelection->GetPresentation(ev), updateShape);    
  231.  
  232.         // ----- Invalidate -----
  233.         fFrame->GetPresentation(ev)->Invalidate(ev, updateShape);
  234.         
  235.         // ----- If I am not the active part I should not have a selection -----
  236.         if (!fContainerPart->IsActive(ev))
  237.             fContainerSelection->CloseSelection(ev);
  238.     }
  239.     
  240.     return result;
  241. }
  242.  
  243. //----------------------------------------------------------------------------------------
  244. //    CDropCommand::DoDroppedInSameFrame
  245. //----------------------------------------------------------------------------------------
  246. FW_Boolean CDropCommand::DoDroppedInSameFrame(Environment* ev, 
  247.                                               ODStorageUnit* dropSU, 
  248.                                               const FW_CPoint& originPoint,
  249.                                               const FW_CPoint& dropPoint)
  250. {
  251.     FW_UNUSED(dropSU);
  252.     
  253.     fDropDelta = dropPoint - originPoint;
  254.     this->OffsetSelection(ev, fDropDelta);
  255.  
  256.     return TRUE;
  257. }
  258.  
  259. //----------------------------------------------------------------------------------------
  260. //    CDropCommand::SaveRedoState
  261. //----------------------------------------------------------------------------------------
  262. void CDropCommand::SaveRedoState(Environment* ev)    // Override
  263. {
  264.     if (fCommandID != kODCommandPasteAs)    // Paste As doesn't need to save shapes
  265.     {
  266.         // Save shapes that were just dropped
  267.         FW_COrderedCollectionIterator ite(fContainerSelection->GetSelectionCollection());
  268.         for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  269.         {
  270.             fDroppedShapeList->AddLast(shape);
  271.         }
  272.     }
  273.  
  274.     // Set Undo/Redo menu strings
  275.     if (fCommandID == kODCommandPasteAs)
  276.         this->SetMenuStrings(ev, "Undo Paste As", "Redo Paste As");
  277.     else if (this->IsDragMoveInSameFrame(ev))
  278.         this->SetMenuStrings(ev, "Undo Move", "Redo Move");
  279.     else
  280.         this->SetMenuStrings(ev, "Undo Drop", "Redo Drop");
  281. }
  282.  
  283. //----------------------------------------------------------------------------------------
  284. //    CDropCommand::UndoIt
  285. //----------------------------------------------------------------------------------------
  286. void CDropCommand::UndoIt(Environment* ev)    // Override
  287. {
  288.     if (fCommandID == kODCommandPasteAs)
  289.     {
  290.         GetContainerLinkManager(ev)->UndoPasteAs(ev, fSavedLink);
  291.     }
  292.     else if (this->IsDragMoveInSameFrame(ev))    // it's a drag-move
  293.     {
  294.         // select dropped shapes and move them back to the original position
  295.         this->SelectDroppedShapes(ev);
  296.         this->OffsetSelection(ev, -fDropDelta);
  297.         fContainerSelection->SelectionChanged(ev);    // need more than this for recontainer???
  298.     }
  299.     else
  300.     {
  301.         // select dropped shapes and remove them from the document
  302.         this->SelectDroppedShapes(ev);
  303.         fContainerSelection->ClearSelection(ev);
  304.     }
  305. }
  306.  
  307. //----------------------------------------------------------------------------------------
  308. //    CDropCommand::RedoIt
  309. //----------------------------------------------------------------------------------------
  310. void CDropCommand::RedoIt(Environment* ev)    // Override
  311. {
  312.     if (fCommandID == kODCommandPasteAs)
  313.     {
  314.         GetContainerLinkManager(ev)->RedoPasteAs(ev, fSavedLink);
  315.         this->OffsetSelection(ev, -fDropDelta);
  316.     }
  317.     else if (this->IsDragMoveInSameFrame(ev))    // it's a drag-move
  318.     {
  319.         // select dropped shapes and move them to new position
  320.         this->SelectDroppedShapes(ev);
  321.         this->OffsetSelection(ev, fDropDelta);
  322.     }
  323.     else    // dropped shapes are copies
  324.     {
  325.         // add dropped shapes back into document
  326.         this->RestoreDroppedShapes(ev);
  327.         fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
  328.     }
  329.     fContainerSelection->SelectionChanged(ev);
  330. }
  331.  
  332. //----------------------------------------------------------------------------------------
  333. //    CDropCommand::RestoreDroppedShapes
  334. //----------------------------------------------------------------------------------------
  335. void CDropCommand::RestoreDroppedShapes(Environment* ev)
  336. {
  337.     // Add the dropped shapes back into the part
  338.     FW_COrderedCollectionIterator ite(fDroppedShapeList);
  339.     for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  340.     {
  341.         shape->RestoreShape(ev, fContainerPart);
  342.     }
  343.  
  344.     // Add the saved shapes to the selection
  345.     this->SelectDroppedShapes(ev);
  346. }
  347.  
  348. //----------------------------------------------------------------------------------------
  349. //    CDropCommand::SelectDroppedShapes
  350. //----------------------------------------------------------------------------------------
  351. void CDropCommand::SelectDroppedShapes(Environment* ev)
  352. {
  353.     // First, empty the selection
  354.     fContainerSelection->CloseSelection(ev);
  355.  
  356.     // Add the shapes in fDroppedShapeList to the selection
  357.     FW_COrderedCollectionIterator ite(fDroppedShapeList);
  358.     for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  359.     {
  360.         fContainerSelection->AddToSelection(ev, shape, FALSE);
  361.     }
  362. }
  363.  
  364. //----------------------------------------------------------------------------------------
  365. //    CDropCommand::OffsetSelection
  366. //----------------------------------------------------------------------------------------
  367. void CDropCommand::OffsetSelection(Environment* ev, const FW_CPoint& delta)
  368. {
  369.     if (delta != FW_kZeroPoint)
  370.     {
  371.         fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
  372.         fContainerSelection->OffsetSelection(ev, delta.x, delta.y);
  373.         fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
  374.     }
  375. }
  376.  
  377. //----------------------------------------------------------------------------------------
  378. //    CDropCommand::CommitUndone
  379. //----------------------------------------------------------------------------------------
  380. void CDropCommand::CommitUndone(Environment* ev)    // Override
  381. {
  382.     if (fCommandID == kODCommandPasteAs)
  383.     {
  384.         GetContainerLinkManager(ev)->CommitUndone(ev, fSavedLink);
  385.     }
  386.     else
  387.         FW_CDropCommand::CommitUndone(ev);
  388. }
  389.  
  390. void CDropCommand::FreeRedoState(Environment* ev)    // Override
  391. {
  392.     this->SelectDroppedShapes(ev);
  393.     fContainerSelection->DeleteSelection(ev);
  394. }
  395.  
  396. //----------------------------------------------------------------------------------------
  397. //    CDropCommand::GetContainerLinkManager
  398. //----------------------------------------------------------------------------------------
  399. CContainerLinkManager* CDropCommand::GetContainerLinkManager(Environment* ev) const
  400. {
  401.     return (CContainerLinkManager*) fPart->GetLinkManager(ev);
  402. }
  403.  
  404. //----------------------------------------------------------------------------------------
  405. //    CDropCommand::DoDroppedPasteAs
  406. //----------------------------------------------------------------------------------------
  407. void CDropCommand::DoDroppedPasteAs(Environment* ev, 
  408.                                     const FW_CPoint& originPoint, 
  409.                                     const FW_CPoint& dropPoint)
  410. {
  411.     fDropDelta = dropPoint - originPoint;
  412.  
  413.     // Save information about newly-created link
  414.     CContainerLinkManager* linkMgr = GetContainerLinkManager(ev);
  415.     fSavedLink = linkMgr->GetLatestSubscriber(ev);
  416.     fSavedLink->SetUpdateOffset(ev, fDropDelta);
  417.  
  418.     if (fDropDelta != FW_kZeroPoint)
  419.     {
  420.         // invalidate and clear the current selection
  421.         fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
  422.         fContainerSelection->CloseSelection(ev);    // empty the selection
  423.  
  424.         // select the dropped shapes
  425.         linkMgr->SelectSubscribedShapes(ev, fSavedLink);
  426.  
  427.         // offset the dropped shapes
  428.         fContainerSelection->OffsetSelection(ev, fDropDelta.x, fDropDelta.y);
  429.         fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
  430.     }
  431. }
  432.